package cloud.daodao.storm.user.repository;

import cloud.daodao.storm.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author DaoDao
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    /**
     * 查询 User
     *
     * @param username String
     * @return User
     */
    User findByUsername(String username);

    User findByNickname(String nickname);

    /**
     * 查询角色用户
     *
     * @param roleId Long
     * @return List
     */
    @Query("select u from User u left join UserRole as ur on u.id = ur.userId left join Role as r on ur.roleId = r.id where ur.valid = true and u.valid = true and r.id = ?1")
    List<User> findAllByRoleId(Long roleId);

}
